import { ref, computed, watch } from 'vue'
import { useRouter } from 'vue-router'
import { Projects } from '../../core/project/project'
import { Toast } from 'vant';
export const useRisk = (props?: any, initData?: any, emit?: any) => {
    const router = useRouter()


    // 详情页面
    const details = async (project: Project) => {
        router.push({
            path: "/riskDetails",
            query: {
                policyNo: project.policyNo
            }
        });
    }



    let risktList = ref([] as Project[])
    const loading = ref(false)
    const finished = ref(false)
    let page = 1
    const pageNum = 5

    const onLand = () => {
        const getRiskList = async () => {
            const res = await Projects.find({ page: page, pageNum: pageNum })
            risktList.value.push(...res.data)
            loading.value = false
            if (risktList.value.length >= res.total) {
                finished.value = true
            }
        }
        getRiskList()
        page++
    }
    //筛选页面
    let risktTypes = ref([
        { name: '结构工程', key: 'structureEPC' },
        { name: '防水工程', key: 'waterproofEPC' },
        { name: '保温工程', key: 'heatPreservationEPC' },
        { name: '机电安装工程', key: 'electromechanicalEPC' },
        { name: '装修工程', key: 'fitmentEPC' },
    ] as { name: string; key: string; checked: boolean }[])

    let companys = ref([
        { name: '正常技术风险', key: 'normal' },
        { name: '轻微技术风险', key: 'slight ' },
        { name: '严重技术风险', key: 'severity' },
        { name: '中等风险', key: 'middling' },
        { name: '技术保留', key: 'retain' }
    ] as { name: string; key: string; checked: boolean }[])

    let mendStates = ref([
        { name: '待整改', key: 'waiting' },
        { name: '逾期未整改', key: 'overdue' },
        { name: '已整改', key: 'finish' },
    ] as { name: string; key: string; checked: boolean }[])



    const riskTypeCheck = (item: any) => {
        item.checked = !item.checked
    }
    const myScreen = ref(null) as any;
    const show = ref(false);
    const showPopup = () => {
        console.log(myScreen.value.show)
        myScreen.value.show = true;
    };
    const value = ref('');
    const value2 = ref('');
    const onSearch = (val: any) => Toast(val);
    const onCancel = () => Toast('取消');
    const onCancel2 = () => Toast('取消');
    //发现
    const findTime = ref('');
    const findTimeStart = ref('');
    const findTimeEnd = ref('');
    const showcalendar = ref(false);
    const formatDate1 = (date: any) => `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
    const onConfirm = (values: any) => {
        const [start, end] = values;
        showcalendar.value = false;
        findTime.value = `${formatDate1(start)} - ${formatDate1(end)}`;
        findTimeStart.value = formatDate1(start);
        findTimeEnd.value = formatDate1(end);
    };
    //整改 

    const mendTime = ref('');
    const mendTimeStart = ref('');
    const mendTimeEnd = ref('');
    const formatDate2 = (date: any) => `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
    const showcalendar2 = ref(false);
    const onConfirm2 = (values: any) => {
        const [start, end] = values;
        showcalendar2.value = false;
        mendTimeStart.value = `${formatDate2(start)}`;
        mendTimeEnd.value = ` ${formatDate2(end)}`;
        mendTime.value = `${formatDate2(start)}-${formatDate2(end)}`
    };


    const defaultDate = ref(new Date())
    const reset = () => {
        risktTypes.value.forEach((item) => {
            item.checked = false
        })
        companys.value.forEach((item) => {
            item.checked = false
        })
        mendStates.value.forEach((item) => {
            item.checked = false
        })
        defaultDate.value = new Date()
    }

    const output = computed(() => {
        let _risktTypes = risktTypes.value.filter((risktType) => {
            return risktType.checked == true
        }).map((risktType) => { return risktType.key })

        let _companys = companys.value.filter((company) => {
            return company.checked == true
        }).map((company) => { return company.key })
        let _mendStates = mendStates.value.filter((mendState) => {
            return mendState.checked == true
        }).map((mendState) => { return mendState.key })
        let _findTime = { findTimeStart: findTimeStart.value, findTimeEnd: findTimeEnd.value }
        let _mendTime = { mendTimeStart: mendTimeStart.value, mendTimeEnd: mendTimeEnd.value }
        return { _risktTypes, _companys, _mendStates, _findTime, _mendTime }

    })
    const determine = () => {
        // console.log(output.value)
        show.value = false;
        emit("determine", output.value)
    }

    const outputData = (outPut: { value: any; }) => {
        console.log(outPut)
    }

    return {
        output,
        mendTime,
        mendTimeStart,
        mendTimeEnd,
        showcalendar,
        showcalendar2,
        defaultDate,
        findTime,
        findTimeStart,
        findTimeEnd,
        reset,
        determine,
        onConfirm,
        onConfirm2,
        details,
        onLand,
        loading,
        finished,
        risktList,
        risktTypes,
        companys,
        mendStates,
        show,
        value,
        value2,
        showPopup,
        onSearch,
        onCancel,
        onCancel2,
        riskTypeCheck,
        myScreen,
        useRisk,
        outputData,

    }
}
